//
//  ViewController.m
//  CKDesignPattern
//
//  Created by 开发 on 2017/6/12.
//  Copyright © 2017年 开发. All rights reserved.
//

#import "ViewController.h"
#import "UIViewController+DesignPattern.h"

static NSString * g_cellId = @"PatternCellID";

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) DesignPatternCellType cellType;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initWidgets];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)loadData{
    self.cellType = patternCommander;
}

- (void)initWidgets{
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//        UITableView *tableView = nil;
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        tableView;
    });
}

#pragma mark - UIScrollViewDelegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellType;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:g_cellId];
    NSString *str = [self getStrByPatternType:self.cellType];
    cell.textLabel.text = str;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *patternVC = [UIViewController new];
    switch (self.cellType) {
        case patternCommander:{
            [patternVC setDesignPatternType:self.cellType];
            [self.navigationController pushViewController:patternVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (NSString *)getStrByPatternType:(DesignPatternCellType)type{
    NSString *str = nil;
    switch (type) {
        case patternCommander:
            str = @"命令模式";
            break;
        default:
            break;
    }
    return str;
}

@end
